package com.sdjz.club.club.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdjz.club.club.entity.Event;
import com.sdjz.club.club.entity.Organization;
import com.sdjz.club.club.entity.UserOrganization;
import com.sdjz.club.club.mapper.OrganizationMapper;
import com.sdjz.club.club.mapper.UserOrganizationMapper;
import com.sdjz.club.club.service.impl.OrganizationServiceImp;
import com.sdjz.club.club.utils.ImageUtil;
import com.sdjz.club.club.utils.SnowFlow;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dftre
 * @since 2022-07-15
 */
//需要进行处理 加上一个视频字段 考虑如何进行传递参数进行处理
@RestController
@RequestMapping("/club/organization")
public class OrganizationController {

    @Autowired
    OrganizationMapper organizationMapper;
    @Autowired
    OrganizationServiceImp organizationServiceImp;
    @Autowired
    UserOrganizationMapper userOrganizationMapper;

    //查询
    @GetMapping("/select")
    public CommonResult select(
            @RequestParam(name = "id", required = false, defaultValue = "0") Long id,
            @RequestParam(name = "name", required = false, defaultValue = "null") String name,
            @RequestParam(name = "proprieter", required = false, defaultValue = "null") String proprieter,
            @RequestParam(name = "type", required = false, defaultValue = "") List<String> type,
            @RequestParam(name = "department", required = false, defaultValue = "1") Integer statu
    ) {

        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        if (statu.equals(1)) wrapper.eq("statu", statu);
        if (!id.equals(0L)) wrapper.eq("id", id);
        if (!name.equals("null")) wrapper.like("name", name);
        if (!proprieter.equals("null")) wrapper.like("proprieter", proprieter);
        if (type.size() != 0) wrapper.in("type", type);
        return new CommonResult(organizationMapper.selectList(wrapper));
    }

    @RequestMapping("/add")
    public CommonResult add(
            @RequestParam("userId") Long userId,
            @RequestParam("name") String name,
            @RequestParam("proprieter") String proprieter,
            @RequestParam("proprieter_phone") String proprieter_phone,
            @RequestParam("img") MultipartFile img,
            @RequestParam("type") String type,
            @RequestParam("introduce") String introduce
    ) {
        Long id = SnowFlow.getId();
        Organization organization = new Organization();
        organization.setId(id);
        organization.setName(name);
        organization.setProprieter(proprieter);
        organization.setProprieterPhone(proprieter_phone);
        organization.setIntroduce(introduce);
        organization.setType(type);
        organization.setImgId(id);
        try {
            ImageUtil.updateImg(img, id);
        } catch (IOException e) {
            return CommonResult.fail("储存图片失败");
        }
        organization.setStatu(0);
        organizationMapper.insert(organization);
        UserOrganization userOrganization = new UserOrganization();
        userOrganization.setOrganizationId(id);
        userOrganization.setUserId(userId);
        userOrganization.setRole("president");
        userOrganizationMapper.insert(userOrganization);
        return CommonResult.succ();
    }


    // 实现分页查询
    @GetMapping("list")
    public CommonResult selectPage(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size,
            @RequestParam(name = "isPending", required = false, defaultValue = "pending") String pending
    ) {
        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        if (pending.equals("pending")) wrapper.eq("statu", 0);
        else wrapper.eq("statu", 1);
        Page<Organization> page = new Page<>(current, size);
        organizationMapper.selectPage(page, wrapper);
        return new CommonResult(page.getRecords());
    }

    // 实现分页查询
    @GetMapping("listV2")
    public CommonResult selectPageV2(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size
    ) {
        Page<Organization> page = new Page<>(current, size);
        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        wrapper.eq("statu", 1);
        List<Map> mapList = new ArrayList<>();
        organizationMapper.selectPage(page, wrapper);
        page.getRecords().forEach(i -> {
            Map map = JSON.parseObject(JSON.toJSONString(i), new TypeReference<Map<String, Object>>() {
            });
            map.put("imgId", "https://www.codingyt.com/getImage/" + map.get("imgId"));
            mapList.add(map);
        });
        return new CommonResult(mapList);
    }

    // 实现删除
    @PostMapping("/delete")
    public CommonResult deleteMenu(@RequestParam("id") String id) {
        return CommonResult.succ(organizationServiceImp.delete(id));
    }

    // 实现更新
    @PostMapping("/update")
    public CommonResult updateMenu(
            @RequestBody Organization organization
//            @RequestParam(name = "img",required = false,defaultValue = "") MultipartFile img
    ) {
//        if(img.isEmpty()){
//            Long id = SnowFlow.getId();
//            try {
//                ImageUtil.updateImg(img, id);
//                organization.setImgId(id);
//            } catch (IOException e) {
//                return CommonResult.fail("储存图片失败");
//            }
//        }
        return CommonResult.succ(organizationMapper.updateById(organization));
    }

}

